package com.hotchpotch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hotchpotch.R;
import com.hotchpotch.inter.OnRecyclerViewItemClickListener;

/**
 * Created by Administrator on 2017/7/12 0012.
 */

public class EncryptAdapter extends RecyclerView.Adapter implements View.OnClickListener, View.OnLongClickListener{
    private Context context;
    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    private String[] items = {"MD5加密","DES加/解密", "BASE64加/解密"};
    private int pos = 0;

    public EncryptAdapter(Context context) {
        this.context = context;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener onItemClickListener) {
        this.onRecyclerViewItemClickListener = onItemClickListener;
    }

    public void setPosition(int position) {
        this.pos = position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_encrypt, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).textView.setText(items[position]);
            if (pos == position) {
                ((ViewHolder) holder).textView.setTextColor(context.getResources().getColor(R.color.colordf262c));
            } else {
                ((ViewHolder) holder).textView.setTextColor(context.getResources().getColor(R.color.text_black));
            }
        }
    }

    @Override
    public int getItemCount() {
        return items.length;
    }

    @Override
    public void onClick(View view) {
        if (onRecyclerViewItemClickListener != null) {
            onRecyclerViewItemClickListener.onItemClick(view);
        }
    }

    @Override
    public boolean onLongClick(View view) {
        if (onRecyclerViewItemClickListener != null) {
            onRecyclerViewItemClickListener.onItemLongClick(view);
        }
        return false;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.item_encrypt_text);
        }
    }
}
